﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.Collections.Generic;

namespace XCI.Core
{
    /// <summary>
    /// 分页列表
    /// </summary>
    /// <typeparam name="T">元素类型</typeparam>
    public class PagedList<T> : List<T>
    {
        /// <summary>
        /// 空列表
        /// </summary>
        public static readonly PagedList<T> Empty = new PagedList<T>(0, 10, 0, null);

        private readonly PagedInfo _pagingCollection;

        /// <summary>
        /// 构造分页列表
        /// </summary>
        /// <param name="pageIndex">页码索引,从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="source">当前页的列表对象</param>
        public PagedList(int pageIndex, int pageSize, int totalCount, IList<T> source)
        {
            _pagingCollection = new PagedInfo(pageIndex, pageSize, totalCount);
            if (source != null)
            {
                AddRange(source);
            }
        }

        /// <summary>
        /// 构造分页列表
        /// </summary>
        /// <param name="page">分页集合对象</param>
        /// <param name="source">当前页的列表对象</param>
        public PagedList(PagedInfo page, IEnumerable<T> source)
        {
            this._pagingCollection = page;
            if (source != null)
            {
                AddRange(source);
            }
        }

        /// <summary>
        /// 页码索引
        /// </summary>
        public int PageIndex
        {
            get { return _pagingCollection.PageIndex; }
        }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize
        {
            get { return _pagingCollection.PageSize; }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount
        {
            get { return _pagingCollection.TotalCount; }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages
        {
            get { return _pagingCollection.TotalPages; }
        }

        /// <summary>
        /// 记录开始数
        /// </summary>
        public int RecordStartIndex
        {
            get { return _pagingCollection.RecordStartIndex; }
        }

        /// <summary>
        /// 记录结束数
        /// </summary>
        public int RecordEndIndex
        {
            get { return _pagingCollection.RecordEndIndex; }
        }

        /// <summary>
        /// 是否首页
        /// </summary>
        public bool IsFirst
        {
            get { return _pagingCollection.IsFirst; }
        }

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool HasPreviousPage
        {
            get { return _pagingCollection.HasPreviousPage; }
        }

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool HasNextPage
        {
            get { return _pagingCollection.HasNextPage; }
        }

        /// <summary>
        /// 是否最后一页
        /// </summary>
        public bool IsLast
        {
            get { return _pagingCollection.IsLast; }
        }
    }

    /// <summary>
    /// 分页信息
    /// </summary>
    public class PagedInfo
    {
        private int _pageIndex;
        private int _pageSize;
        private int _recordEndIndex;
        private int _recordStartIndex;
        private int _totalCount;
        private int _totalPages;

        /// <summary>
        /// 构造分页集合
        /// </summary>
        /// <param name="pageIndex">页码索引,从1开始</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="totalCount">总记录数</param>
        public PagedInfo(int pageIndex, int pageSize, int totalCount)
        {
            _pageIndex = pageIndex;
            _pageSize = pageSize;
            _totalCount = totalCount;
            _totalPages = (totalCount + pageSize - 1) / pageSize;
        }

        /// <summary>
        /// 页码索引
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex; }
        }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount
        {
            get { return _totalCount; }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages
        {
            get { return _totalPages; }
        }

        /// <summary>
        /// 当前记录开始数
        /// </summary>
        public int RecordStartIndex
        {
            get
            {
                if (_totalCount > 0)
                {
                    _recordStartIndex = (_pageIndex - 1) * PageSize + 1;
                }

                return _recordStartIndex;
            }
        }

        /// <summary>
        /// 当前记录结束数
        /// </summary>
        public int RecordEndIndex
        {
            get
            {
                _recordEndIndex = _recordStartIndex + _pageSize - 1;
                if (_recordEndIndex > _totalCount)
                {
                    _recordEndIndex = _totalCount;
                }

                return _recordEndIndex;
            }
        }

        /// <summary>
        /// 是否首页
        /// </summary>
        public bool IsFirst
        {
            get { return (_pageIndex == 0); }
        }

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool HasPreviousPage
        {
            get { return (_pageIndex > 0); }
        }

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool HasNextPage
        {
            get { return (_pageIndex + 1 < _totalPages); }
        }

        /// <summary>
        /// 是否最后一页
        /// </summary>
        public bool IsLast
        {
            get { return (_pageIndex == _totalPages - 1); }
        }
    }
}